﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using CCWin;
using Newtonsoft.Json;

namespace SvipQuery
{
    public partial class FrmMain : Skin_Mac
    {
        private HttpHelpers helpers = new HttpHelpers();
        private XJHTTP xjHttp = new XJHTTP();
        private bool extractFlag;
        private LoginUser User;
        public FrmMain(LoginUser loginUser)
        {
            InitializeComponent();
            User = loginUser;
        }

        #region >>>>其他事件

        private void lblLZ_MouseMove(object sender, MouseEventArgs e)
        {
            Cursor.Current = Cursors.Hand;
        }
        private void EnabledExtract()
        {
            开始查询ToolStripMenuItem.Enabled = !extractFlag;
            粘贴导入ToolStripMenuItem.Enabled = !extractFlag;
            文本导入ToolStripMenuItem.Enabled = !extractFlag;
            清空账号ToolStripMenuItem.Enabled = !extractFlag;
            全部选择ToolStripMenuItem.Enabled = !extractFlag;
            反向选择ToolStripMenuItem.Enabled = !extractFlag;
        }
        private void lblMe_Click(object sender, EventArgs e)
        {
            Process.Start("tencent://AddContact/?fromId=45&fromSubId=1&subcmd=all&uin=935411557&website=www.silkage.net");
        }

        private void lblVersion_Click(object sender, EventArgs e)
        {
            Process.Start("https://blog.silkage.net");
        }

        private void 粘贴导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var data = Clipboard.GetDataObject();
            if (data == null || !data.GetDataPresent(DataFormats.Text)) return;
            var dataTxt = (string)data.GetData(DataFormats.Text);
            var stream = new MemoryStream();
            var writer = new StreamWriter(stream);
            writer.Write(dataTxt);
            writer.Flush();
            stream.Position = 0;
            using (var sr = new StreamReader(stream))
            {
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Trim() == "") continue;
                    if (!long.TryParse(str, out long qq))
                        continue;
                    int index = dgvAccount.Rows.Add();
                    dgvAccount[0, index].Value = true;
                    dgvAccount[1, index].Value = qq;
                }
            }
        }

        private void 文本导入ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (openFileDialog.ShowDialog() != DialogResult.OK)
                return;
            using (var sr = new StreamReader(openFileDialog.FileName))
            {
                string str;
                while ((str = sr.ReadLine()) != null)
                {
                    if (str.Trim() == "") continue;
                    if (!long.TryParse(str, out long qq))
                        continue;
                    int index = dgvAccount.Rows.Add();
                    dgvAccount[0, index].Value = true;
                    dgvAccount[1, index].Value = qq;
                }
            }
        }

        private void 清空账号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            dgvAccount.Rows.Clear();
        }

        private void 全部选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvAccount.RowCount == 0)
                return;
            for (int i = dgvAccount.RowCount - 1; i >= 0; i--)
            {
                dgvAccount[0, i].Value = true;
            }
        }

        private void 反向选择ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvAccount.RowCount == 0)
                return;
            for (int i = dgvAccount.RowCount - 1; i >= 0; i--)
            {
                var flag = Convert.ToBoolean(dgvAccount[0, i].Value);
                dgvAccount[0, i].Value = !flag;
            }
        }

        private void lblLZ_Click(object sender, EventArgs e)
        {
            Process.Start("https://jq.qq.com/?_wv=1027&k=5OhuxMP");
        }
        #endregion

        private void 暂停查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            extractFlag = false;
            EnabledExtract();
        }

        private void 开始查询ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (dgvAccount.RowCount == 0)
                return;
            extractFlag = true;
            EnabledExtract();
            ThreadPool.QueueUserWorkItem(s =>
            {
                for (var i = 0; i < dgvAccount.RowCount; i++)
                {
                    if (!extractFlag)
                        return;
                    if (!Convert.ToBoolean(dgvAccount[0, i].Value))
                        continue;
                    var account = new Account
                    {
                        Qq = dgvAccount[1, i].Value?.ToString(),
                        Index = i
                    };
                    Query(account);
                    Invoke(new MethodInvoker(delegate
                    {
                        dgvAccount[0, i].Value = false;
                    }));
                    Thread.Sleep(1000);
                }
                Invoke(new MethodInvoker(delegate
                {
                    extractFlag = false;
                    EnabledExtract();
                }));
            });
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="account"></param>
        private void Query(Account account)
        {
            try
            {
                var item = new HttpItems
                {
                    URL = $"http://vipfunc.qq.com/common/user.php?callback=jQuery17206153543894179165_1419173713842&data=uin,is_login,face_url,nick_name,is_club,is_superclub,is_year_club,level,grow_value,add_step,bank_pay,pay_way,mobile_pay_way,club_end_time,today,has_error,grow_rank&f_uin={account.Qq}&f_data=uin,is_login,face_url,nick_name,is_club,is_superclub,is_year_club,level,grow_value,add_step,bank_pay,pay_way,mobile_pay_way,club_end_time,today,has_error,grow_rank&minipk=1&g_tk={User.Gtk32}&_=1419173713902",
                    Cookie = User.Cookies,
                    Referer = $"http://vipfunc.qq.com/common/user.php?callback=jQuery17206153543894179165_1419173713842&data=uin,is_login,face_url,nick_name,is_club,is_superclub,is_year_club,level,grow_value,add_step,bank_pay,pay_way,mobile_pay_way,club_end_time,today,has_error,grow_rank&f_uin={account.Qq}&f_data=uin,is_login,face_url,nick_name,is_club,is_superclub,is_year_club,level,grow_value,add_step,bank_pay,pay_way,mobile_pay_way,club_end_time,today,has_error,grow_rank&minipk=1&g_tk={User.Gtk32}&_=1419173713902"
                };
                var res = helpers.GetHtml(item);
                var html = res.Html.Replace("jQuery17206153543894179165_1419173713842(", "").Replace(");", "");
                var obj = JsonConvert.DeserializeObject<List<VipInfo>>(html);
                if (obj == null || obj.Count != 2)
                {
                    return;
                }
                var info = obj[1];
                Invoke(new MethodInvoker(delegate
                {
                    dgvAccount[2, account.Index].Value = info.nick_name;
                    dgvAccount[3, account.Index].Value = (info.is_club == 1) ? ((info.is_year_club == 1 ? "年费" : "") + (info.is_superclub == 1 ? "超级" : "") + "会员" + info.level + "级") : "非会员";
                    dgvAccount[4, account.Index].Value = info.add_step;
                    dgvAccount[5, account.Index].Value = info.grow_value;
                    dgvAccount[6, account.Index].Value = info.club_end_time;
                }));
            }
            catch (Exception ex)
            {
                Invoke(new MethodInvoker(delegate
                {
                    MessageBox.Show(ex.Message);
                }));
            }
        }
    }

    public class VipInfo
    {
        public bool has_error { get; set; }
        public string error_msg { get; set; }
        public long uin { get; set; }
        public string face_url { get; set; }
        public string nick_name { get; set; }
        public int is_club { get; set; }
        public int is_superclub { get; set; }
        public int is_year_club { get; set; }
        public string level { get; set; }
        public string grow_value { get; set; }
        public string add_step { get; set; }
        public int bank_pay { get; set; }
        public string pay_way { get; set; }
        public string mobile_pay_way { get; set; }
        public string club_end_time { get; set; }
        public string today { get; set; }
        public object grow_rank { get; set; }
        public int servertime { get; set; }
        public int is_login { get; set; }
        public int is_friend { get; set; }
    }

}
